/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.service.cmd;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;

/**
 * 建立日期 : 2013-6-7 上午9:35:41<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class SubmitResult extends Result {

	private String type;

	public final static String TYPE_END = "end";// 工作结束

	public final static String TYPE_NEXT = "next";// 进入下面环节

	public final static String TYPE_PAUSE_SEQ = "pause_seq";// 本环节，顺序

	public final static String TYPE_PAUSE_CONCURRENCE = "pause_concurrence";// 本环节，并发

	public final static String TYPE_WAITING_MULTIout = "waiting_multiOut";// 等待汇聚，多路径
	
	public final static String TYPE_FREE = "free";
	
	public final static String TYPE_FREE_END = "free_end";

	public final static String TYPE_ERROR = "error";

	private Map<String, List<User>> nextActors;

	private String nextActor;

	public String getType() {
		return type;
	}

	public boolean isEnd() {
		return this.type.equals(TYPE_END);
	}

	public boolean isNext() {
		return this.type.equals(TYPE_NEXT);
	}

	public boolean isPauseSeq() {
		return this.type.equals(TYPE_PAUSE_SEQ);
	}

	public boolean isPauseConcurrence() {
		return this.type.equals(TYPE_PAUSE_CONCURRENCE);
	}

	public boolean isWaitingMultiOut() {
		return this.type.equals(TYPE_WAITING_MULTIout);
	}

	public boolean isFreeResult() {
		return this.type.equals(TYPE_FREE)||this.type.equals(TYPE_FREE_END);
	}

	public void setType(String type) {
		this.type = type;
	}

	public Map<String, List<User>> getNextActors() {
		return nextActors;
	}

	public void setNextActors(Map<String, List<User>> nextActors) {
		this.nextActors = nextActors;
	}

	public String getNextActor() {
		return nextActor;
	}

	public void setNextActor(String nextActor) {
		this.nextActor = nextActor;
	}

	public String spellNextMsg() {
		String message = "";
		if (nextActors != null) {
			Set<String> keys = nextActors.keySet();
			String nMsg = "流程已提交到";
			for (String key : keys) {
				String iStr = "[ " + key + ":";
				List<User> us = nextActors.get(key);
				IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
				for (User u : us) {
					iStr = iStr + idService.getUserById(u.getId()).getName() + ",";
				}

				if (iStr.endsWith(",")) {

					iStr = iStr.substring(0, iStr.length() - 1);
					iStr = iStr + " ]";

				} else {
					iStr = iStr + " 无处理人 ]";
				}

				nMsg = nMsg + iStr;
			}

			if (nMsg.endsWith("]")) {

			} else {
				nMsg = "流程已提交！";
			}
			message = nMsg;

		}
		return message;
	}

	public String spellNextMsgForFree() {
		String message = null;
		if (nextActors != null) {
			Set<String> keys = nextActors.keySet();
			List<User> us = nextActors.get(keys.iterator().next());

			String iStr = "[";

			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			for (User u : us) {
				iStr = iStr + idService.getUserById(u.getId()).getName() + ",";
			}

			if (iStr.endsWith(",")) {

				iStr = iStr.substring(0, iStr.length() - 1);
				iStr = iStr + "]";
				message = iStr;
			}
		}
		return message;
	}

}
